import { Courses } from "../../models/Course"
import { isEmpty } from "../../utils"

// 验证创建的表单参数
export function validateCreateForm(form: Courses) {
  let message = ''
  const params = new Map([
    ['course_name', '班级名称'],
    ['teacher_name', '授课教师'],
    ['teacher_id', '教师id'],
    ['credit', '学分'],
    ['time_1', '上课时间1'],
    ['time_2', '上课时间2'],
    ['location', '上课地点'],
    ['college_name', '所属学院'],
    ['major_name', '所属专业'],
    ['lesson_period', '课时'],
    ['num_limit', '选课限制人数'],
    ['type', '课程类型'],
    ['choose_limit', '选课限制'],
  ])

  params.forEach((val, key) => {
    if (isEmpty(form[key])) {
      message = `缺少必填参数-${val}!`
      return
    }
  })
  return message
}

// 处理创建表单中的类型
export function handleFormDataType(form: Courses) {
  // 整数类型
  const numberType = ['credit', 'time_1', 'time_2', 'lesson_period', 'type', 'choose_limit', 'num_limit']
  numberType.forEach(item => {
    form[item] = parseInt(form[item])
  })
  // 布尔类型
  const booleanType = ['state']
  booleanType.forEach(item => {
    if (form[item]) {
      form[item] = Boolean(form[item])
    }
  })
  return form
}
